package com.ymatou.iapi.livebuyer.testcase;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.GetLiveProductDotListBean;
import com.ymatou.iapi.livebuyer.service.GetLiveProductDotListCall;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*查询直播间打点商品
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetLiveProductDotList{
	private static GetLiveProductDotListBean getliveproductdotlistBean;
	private static GetLiveProductDotListCall getliveproductdotlistCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询直播间打点商品");
	}

	@Before
	public void caseUp() {
		getliveproductdotlistBean=new GetLiveProductDotListBean();
		getliveproductdotlistCall=new GetLiveProductDotListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getliveproductdotlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetLiveProductDotList_001() {
		Logger.start(true,"查询直播间打点商品成功,置顶过");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Thread.sleep(5000);
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			LiveRoomCallService.SetLiveProductTop(createlive.getId(), productId);
//			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
//			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
//			CIWapper.updatestocknumByProductID(0, productId);
			getliveproductdotlistBean.setLiveId(createlive.getId());
			getliveproductdotlistBean.setStayBeginTime(YMTDateUtil.getBeforeOrNextMunite(-1));
			getliveproductdotlistBean.setStayEndTime(YMTDateUtil.getBeforeOrNextMunite(2));
			getliveproductdotlistCall.setData(getliveproductdotlistBean);
			getliveproductdotlistCall.callService();			
			Logger.verifyEquals("200", getliveproductdotlistCall.getString("code"), "验证code");
			Logger.verifyEquals(1, getliveproductdotlistCall.getLiveProductDotDtos().size(), "展示商品");
			Logger.verifyEquals(productId,getliveproductdotlistCall.getLiveProductDotDtos().get(0).getProductId(), "验证productId");
			Logger.verifyEquals(1,getliveproductdotlistCall.getLiveProductDotDtos().get(0).getStatus(), "验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetLiveProductDotList_002() {
		Logger.start(true,"不输入liveId失败");
		try {
//			getliveproductdotlistBean.setLiveId(1);
			getliveproductdotlistBean.setStayBeginTime(YMTDateUtil.getBeforeOrNextMunite(-1));
			getliveproductdotlistBean.setStayEndTime(YMTDateUtil.getBeforeOrNextMunite(2));
			getliveproductdotlistCall.setData(getliveproductdotlistBean);
			getliveproductdotlistCall.callService();			
			Logger.verifyEquals("100", getliveproductdotlistCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetLiveProductDotList_003() {
		Logger.start(true,"不输入stayBeginTime失败");
		try {
			getliveproductdotlistBean.setLiveId(1);
//			getliveproductdotlistBean.setStayBeginTime(YMTDateUtil.getBeforeOrNextMunite(-1));
			getliveproductdotlistBean.setStayEndTime(YMTDateUtil.getBeforeOrNextMunite(2));
			getliveproductdotlistCall.setData(getliveproductdotlistBean);
			getliveproductdotlistCall.callService();			
			Logger.verifyEquals("100", getliveproductdotlistCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetLiveProductDotList_004() {
		Logger.start(true,"不输入stayEndTime失败");
		try {
			getliveproductdotlistBean.setLiveId(1);
			getliveproductdotlistBean.setStayBeginTime(YMTDateUtil.getBeforeOrNextMunite(-1));
//			getliveproductdotlistBean.setStayEndTime(YMTDateUtil.getBeforeOrNextMunite(2));
			getliveproductdotlistCall.setData(getliveproductdotlistBean);
			getliveproductdotlistCall.callService();			
			Logger.verifyEquals("100", getliveproductdotlistCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetLiveProductDotList_005() {
		Logger.start(true,"查询直播间打点商品成功,录制过");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Thread.sleep(5000);
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			//开始直播录制
			LiveRoomCallService.CreateProductVideo(createlive.getId(), sellerId, productId);
//			LiveRoomCallService.SetLiveProductTop(createlive.getId(), productId);
//			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
//			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
//			CIWapper.updatestocknumByProductID(0, productId);
			getliveproductdotlistBean.setLiveId(createlive.getId());
			getliveproductdotlistBean.setStayBeginTime(YMTDateUtil.getBeforeOrNextMunite(-1));
			getliveproductdotlistBean.setStayEndTime(YMTDateUtil.getBeforeOrNextMunite(2));
			getliveproductdotlistCall.setData(getliveproductdotlistBean);
			getliveproductdotlistCall.callService();			
			Logger.verifyEquals("200", getliveproductdotlistCall.getString("code"), "验证code");
			Logger.verifyEquals(1, getliveproductdotlistCall.getLiveProductDotDtos().size(), "展示商品");
			Logger.verifyEquals(productId,getliveproductdotlistCall.getLiveProductDotDtos().get(0).getProductId(), "验证productId");
			Logger.verifyEquals(1,getliveproductdotlistCall.getLiveProductDotDtos().get(0).getStatus(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	//20201024 @曹二信 商品维度是否露出 上一层判断
	public void Tc_GetLiveProductDotList_006() {
		Logger.start(true,"移除商品不展示");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Thread.sleep(5000);
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			LiveRoomCallService.SetLiveProductTop(createlive.getId(), productId);
			LiveRoomCallService.RemoveLiveProduct(createlive.getId(), productId);
			
			getliveproductdotlistBean.setLiveId(createlive.getId());
			getliveproductdotlistBean.setStayBeginTime(YMTDateUtil.getBeforeOrNextMunite(-1));
			getliveproductdotlistBean.setStayEndTime(YMTDateUtil.getBeforeOrNextMunite(2));
			getliveproductdotlistCall.setData(getliveproductdotlistBean);
			getliveproductdotlistCall.callService();			
			Logger.verifyEquals("200", getliveproductdotlistCall.getString("code"), "验证code");
			Logger.verifyEquals(0, getliveproductdotlistCall.getLiveProductDotDtos().size(), "移除不展示");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	//20201024 @曹二信 商品维度是否露出 上一层判断
	public void Tc_GetLiveProductDotList_007() {
		Logger.start(true,"售罄商品不展示");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			addpro.getProduct().getCatalogList().remove(0);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Thread.sleep(5000);
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			LiveRoomCallService.SetLiveProductTop(createlive.getId(), productId);
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			CIWapper.updatestocknumByProductID(0, productId);
			getliveproductdotlistBean.setLiveId(createlive.getId());
			getliveproductdotlistBean.setStayBeginTime(YMTDateUtil.getBeforeOrNextMunite(-1));
			getliveproductdotlistBean.setStayEndTime(YMTDateUtil.getBeforeOrNextMunite(2));
			getliveproductdotlistCall.setData(getliveproductdotlistBean);
			getliveproductdotlistCall.callService();			
			Logger.verifyEquals("200", getliveproductdotlistCall.getString("code"), "验证code");
			Logger.verifyEquals(0, getliveproductdotlistCall.getLiveProductDotDtos().size(), "售罄不展示");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}